import axios from 'axios'
  export default fetch

  axios.defaults.timeout = 600000;
  axios.interceptors.request.use(
    config => {
      if (config.url == '/web-api/infra/file/videoTranscoding' || config.url == '/web-api/infra/file/inspirationTranscoding') {
        config.headers['Content-Type'] = "multipart/form-data; boundary=something";
      } else if (config.url == '/web-api/member/face/detail' || config.url == '/web-api/member/auth/gzhLogin' || config.url == '/web-api/member/ocr/ocrCardFront'||config.url == '/web-api/business/file/download/getUrl'){
        config.headers['Content-Type'] = 'multipart/form-data';
      }else if( config.url == '/web-api/business/product/favorite/delete'|| config.url == '/web-api/business/product/favorite/create'){
        config.headers['Content-Type'] = 'application/json';
      }else{}
      
      // config.cancelToken = new axios.CancelToken(cancel => { // 在发送请求设置cancel token
      //   store.commit('setAxiosArr', { cancelToken: cancel })
      // })
      if (localStorage.getItem("token")) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
        config.headers.Authorization = localStorage.getItem("token");
      }
      return config;
    },
    err => {
      return Promise.reject(err);
    });
  
  axios.interceptors.response.use(function (res) {  
    // token 已过期，重定向到登录页面  
    if(res.data.code == 302){
        location.href = "#"+res.data.location;
    }
    
    return res;
    
    
}, function (error) {  
    // Do something with response error  
    return Promise.reject(error)  
});

  function fetch(options){
    
    let {method,path,params} = options;
    // console.log(path)
    let request = ()=>{};
    switch(method){
        case('get'):request = get;break;
        case('post'):request = post;break;
        case('put'):request = put;break;
        case('postForm'):request = postForm;break;
        case('delete'):request = deleteF;break;
        // default:request = null;break;
    }


    return request(path,params);

    function get(path,params){
        return new Promise((resolve,reject)=>{
           axios.get(path,{
               params
           }).then(res=>{ 
                    resolve(res.data);

           }).catch(err=>{
                reject(err);
           });
        });
    }

    function post(path,params){
        return new Promise((resolve,reject)=>{
           axios.post(path,params).then(res=>{

                resolve(res.data);

           }).catch(err=>{
                reject(err);
           });
        });
    }
    function postForm(path,params){
        return new Promise((resolve,reject)=>{
           axios.postForm(path,params).then(res=>{
                resolve(res.data);

           }).catch(err=>{
                reject(err);
           });
        });
    }
    function put(path,params){
        return new Promise((resolve,reject)=>{
           axios.put(path,params).then(res=>{
                resolve(res.data);
           }).catch(err=>{
                reject(err);
           });
        });
    }
    function deleteF(path,params){
        return new Promise((resolve,reject)=>{
           axios.delete(path,{params}).then(res=>{
                resolve(res.data);
           }).catch(err=>{
                reject(err);
           });
        });
    }
  }